package org.sws.domain.behavior.service;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.sws.domain.behavior.event.SendRebateMessageEvent;
import org.sws.domain.behavior.model.aggregate.BehaviorRebateAggregate;
import org.sws.domain.behavior.model.entity.BehaviorEntity;
import org.sws.domain.behavior.model.entity.BehaviorRebateConfigEntity;
import org.sws.domain.behavior.model.entity.BehaviorRebateOrderEntity;
import org.sws.domain.behavior.model.entity.BehaviorRebateTaskEntity;
import org.sws.domain.behavior.repository.IBehaviorRebateRepository;
import org.sws.types.common.Constants;
import org.sws.types.enums.TaskState;
import org.sws.types.event.BaseEvent;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author sws
 * @Date 2025/3/27 11:44
 * @description:
 */
@Service
public class BehaviorRebateService implements IBehaviorRebateService{

    @Resource
    private IBehaviorRebateRepository behaviorRebateRepository;
    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;
    @Override
    public List<String> createBehaviorRebateOrder(BehaviorEntity behaviorEntity) {
        // 查询行为返利配置
        List<BehaviorRebateConfigEntity> behaviorRebateConfigs = behaviorRebateRepository.queryBehaviorRebateConfig(behaviorEntity.getBehaviorTypeVO().getCode());
        // 构建聚合对象
        List<String> orderIds = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregates = new ArrayList<>();
        for (BehaviorRebateConfigEntity config : behaviorRebateConfigs) {
            // 拼装业务ID；用户ID_返利类型_外部透彻业务ID
            String bizId = behaviorEntity.getUserId() + Constants.UNDERLINE + config.getRebateType() + Constants.UNDERLINE + behaviorEntity.getOutBusinessNo();
            BehaviorRebateOrderEntity behaviorRebateOrderEntity = BehaviorRebateOrderEntity.builder()
                    .userId(behaviorEntity.getUserId())
                    .orderId(RandomStringUtils.randomNumeric(12))
                    .behaviorType(config.getBehaviorType())
                    .rebateDesc(config.getRebateDesc())
                    .rebateType(config.getRebateType())
                    .rebateConfig(config.getRebateConfig())
                    .bizId(bizId)
                    .build();
            orderIds.add(behaviorRebateOrderEntity.getOrderId());

            // MQ 消息数据对象
            SendRebateMessageEvent.RebateMessage rebateMessage = SendRebateMessageEvent.RebateMessage.builder()
                    .userId(behaviorEntity.getUserId())
                    .rebateType(config.getRebateType())
                    .rebateConfig(config.getRebateConfig())
                    .bizId(bizId)
                    .build();
            // 构建事件消息
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> rebateMessageEventMessage = sendRebateMessageEvent.buildEventMessage(rebateMessage);

            // 组装任务对象
            BehaviorRebateTaskEntity taskEntity = new BehaviorRebateTaskEntity();
            taskEntity.setUserId(behaviorEntity.getUserId());
            taskEntity.setTopic(sendRebateMessageEvent.topic());
            taskEntity.setMessageId(rebateMessageEventMessage.getId());
            taskEntity.setMessage(rebateMessageEventMessage);
            taskEntity.setState(TaskState.create);

            BehaviorRebateAggregate behaviorRebateAggregate = BehaviorRebateAggregate.builder()
                    .userId(behaviorEntity.getUserId())
                    .behaviorRebateOrderEntity(behaviorRebateOrderEntity)
                    .behaviorRebateTaskEntity(taskEntity)
                    .build();

            behaviorRebateAggregates.add(behaviorRebateAggregate);
        }

        // 3. 存储聚合对象数据
        behaviorRebateRepository.saveUserRebateRecord(behaviorEntity.getUserId(), behaviorRebateAggregates);

        // 4. 返回订单ID集合
        return orderIds;
    }

    @Override
    public Boolean isCalendarSignRebate(String userId, String outBusinessNo) {
        return behaviorRebateRepository.isCalendarSignRebate(userId,outBusinessNo);
    }
}
